* 
global path "Y:\Fuyao_Erlfang\estimates\pair"

clear 
edit
* Paste estimate point from excel
gen n=_n
gen m1=additive
*gen m2 =multiplicative
* Model transformations
local i =1
local mod= "additive"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=(`mod') if n==8 //level 1 ELIMINATES 1.4

foreach num of numlist 9/14{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	// ELIMINATES 1.4
}
foreach num of numlist 15/20 {
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 27/31{
replace m`i'=((1.4^`mod')/((1.4^`mod')+1))*4 if n==`num'	 	//by 4
}
saveold "$path/fine_level_add/point_estimates_add_pair_wo.dta", replace

clear
ed
gen n=_n
*gen m1=additive
gen m2 =multiplicative
local i =2
local mod= "multiplicative"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=1+(`mod') if n==8 //1+level 2// ELIMINATES 1.4

foreach num of numlist 9/13{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	// ELIMINATES 1.4
}
foreach num of numlist 14/19 26{
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 27/31{
replace m`i'=((1.4^`mod')/((1.4^`mod')+1))*4 if n==`num'	 	//by 4
}

saveold "$path/fine_level_mul/point_estimates_mul_pair_wo.dta", replace
 
 * Additive Model
global path "Y:\Fuyao_Erlfang\estimates\pair"
*global path "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/finemotor/fine_add"

 use "$path/fine_level_add/bootsresults.dta", clear 
 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) //Eliminates 1.4
foreach num of numlist 9/14{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Eliminates 1.4	
}
foreach num of numlist 15/20 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var20_2 var21-var26 var27_2-var31_2 var32-var40{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var18_2  {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 21/26 32/40{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/20 27/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q

rename numero n
merge m:1 n using "$path/fine_level_add/point_estimates_add_pair_wo.dta"
gen out= 1 if m1 > var_ub_ | m1 <var_lb_
br 
pause
drop out 

tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t = "(" + var_sd_ + ")" if q==2
tostring m1 , replace force format(%6.3f)
replace t=m1  if q ==1

keep q n var1 t
order q n var1 t
replace var1="" if q==2| q==3
saveold "$path/fine_level_add/tempf_add_pair_wo.dta" , replace
gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t 
drop q n
texsave using "$path/fine_level_add/Fine_add_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|c) location(ht) varlabels frag ///
    title(Fine Additive) marker(tab:Fine_add)  ///
    footnote("Note:", size(scriptsize)) autonumber

	*Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_add/initial.out


*Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_add/Lang_add.tex
global path "Y:\Fuyao_Erlfang\estimates\pair"

* Multiplicative
use "$path/fine_level_mul/bootsresults.dta", clear 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //Eliminates 1.4
foreach num of numlist 9/13{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Eliminates 1.4	
}
foreach num of numlist 14/19 26{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var19_2 var20-var25 var26_2-var31_2 var32-var40{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var17_2 var18_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}

foreach var of varlist var19_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<993
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 20/25 32/40{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/19 26/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/fine_level_mul/point_estimates_mul_pair_wo.dta"
gen out= 1 if m2 > var_ub_ | m2 <var_lb_
br 
pause
drop out 

tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t2 = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t2= "(" + var_sd_ + ")" if q==2
tostring  m2, replace force format(%6.3f)
replace t2=m2 if q ==1
keep q n var1 t2
order q n var1 t2
replace var1="" if q==2| q==3
saveold "$path/fine_level_mul/tempf_multi_pair_wo.dta", replace 

gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t2

drop q n  
*paste manually the levels in a new column
texsave using "$path/fine_level_mul/Fine_Mult_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(llcllc) location(ht) varlabels frag ///
    title(Fine Motor Multiplicative) marker(tab:fine_)  ///
    footnote("Note:", size(scriptsize)) autonumber


